package com.csd.action.news;

import java.util.ArrayList;
import java.util.List;

import com.csd.helper.IConstant;
import com.csd.pojo.Message;
import com.csd.pojo.News;
import com.csd.service.INewsService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Action:列出所有的新闻
 * 
 * @author csd
 * @version 1.0
 * 
 * <br>
 *          修订时间：2009-12-17 <br>
 *          修改原因：创建文件 <br>
 *          修改人：何文深 <br>
 * 
 */
public class NewsListAction {
	
	private Integer numInPage;

	private Integer currPage;
	
	private Integer totalPage;

	public Integer getNumInPage() {
		return numInPage;
	}

	public void setNumInPage(Integer numInPage) {
		this.numInPage = numInPage;
	}

	public Integer getCurrPage() {
		return currPage;
	}

	public void setCurrPage(Integer currPage) {
		this.currPage = currPage;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	private List<News> newsList;
	
	private INewsService newsService;

	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public INewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}
	
	public String execute() throws Exception {
		String text = "fail";
		
		List<News> old = newsService.listNews();
		
		if( this.currPage == null  || this.currPage <= 0 )
			this.currPage = 1;
		
		if( this.numInPage == null || this.numInPage <= 0)
			this.numInPage = IConstant.numInPage;
		
		if(newsList == null)
		{
			newsList = new ArrayList<News>();
		}
		
		if(old == null)
		{
			old = new ArrayList<News>();
		}
		
		newsList.clear();
		
		for(int i = (this.currPage-1)*this.numInPage; 
				i < this.currPage*this.numInPage && i < old.size(); 
				i++)
		{
			newsList.add( old.get(i) );
		}
		
		this.totalPage = old.size() / this.numInPage;
		
		if(old.size() % this.numInPage != 0)
			this.totalPage++;
		
		text = "success";
		return text;
	}
}
